﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HslCommunication.ModBus;
using HslCommunication;
using System.IO.Ports;
using HslCommunication.Core;
using System.Collections;

namespace HslDemo
{
    class ModbusRtuMainStation
    {
        private HslCommunication.ModBus.ModbusRtu _BusRtuClient;
        ushort _Station;
        string _PortName;
        int _BaudRate;
        int _DataBits;
        StopBits _StopBits;
        Parity _Parity;
        DataFormat _DataFormat;
        public ModbusRtuMainStation(ushort Station, string PortName, int BaudRate, int DataBits, StopBits StopBits, Parity ParityMess, string DataForamtType)
        {
            _Station = Station;
            _PortName = PortName;
            _BaudRate = BaudRate;
            _DataBits = DataBits;
            _StopBits = StopBits;
            _Parity = ParityMess;
            switch (DataForamtType)
            {
                case "ABCD":
                    _DataFormat = DataFormat.ABCD;
                    break;
                case "BADC":
                    _DataFormat = DataFormat.BADC;
                    break;
                case "CDAB":
                    _DataFormat = DataFormat.CDAB;
                    break;
                case "DCBA":
                    _DataFormat = DataFormat.DCBA;
                    break;
                default:
                    _DataFormat = DataFormat.ABCD;
                    break;
            }

        }
        public bool Open()
        {
            try
            {
                _BusRtuClient = new HslCommunication.ModBus.ModbusRtu((byte)_Station);
                _BusRtuClient.SerialPortInni(sp =>
                {
                    sp.PortName = _PortName;
                    sp.BaudRate = _BaudRate;
                    sp.DataBits = _DataBits;
                    sp.StopBits = _StopBits;
                    sp.Parity = _Parity;
                });
                _BusRtuClient.Open();
                return true;
            }
            catch (Exception)
            {
                return false;
            }

        }
        public bool Close()
        {
            try
            {
                _BusRtuClient.Close();
                return true;
            }
            catch (Exception)
            {
                return false;
            }

        }

        #region 读
        /// <summary>
        /// 读
        /// </summary>
        /// <param name="DataType">数值类型</param>
        /// <param name="FuncValue">功能码</param>
        /// <param name="Adress">地址</param>
        /// <param name="Count">字符串个数，读字符串才填写</param>
        /// <returns></returns>
        public object Read(ModbusRtuDataType DataType, int FuncValue, uint Adress, ushort Count = 0)
        {
            //FuncValue功能码
            //0x01    读取线圈的操作，
            //0x02    读取离散的操作，
            //0x03    读取寄存器的值，
            //0x04    读取输入寄存器的值,
            _BusRtuClient.DataFormat = _DataFormat;//格式
            string FuncAndAdress = "x=" + FuncValue + ";" + Adress;
            try
            {
                switch (DataType)
                {
                    case ModbusRtuDataType.ModbusTcpBool:
                        bool BoolRes = _BusRtuClient.ReadBool(FuncAndAdress).Content;
                        return BoolRes;
                    case ModbusRtuDataType.ModbusTcpInt16:
                        short ShortRes = _BusRtuClient.ReadInt16(FuncAndAdress).Content;
                        return ShortRes;
                    case ModbusRtuDataType.ModbusTcpUInt16:
                        ushort UShortRes = _BusRtuClient.ReadUInt16(FuncAndAdress).Content;
                        return UShortRes;
                    case ModbusRtuDataType.ModbusTcpInt32:
                        int IntRes = _BusRtuClient.ReadInt32(FuncAndAdress).Content;
                        return IntRes;
                    case ModbusRtuDataType.ModbusTcpUInt32:
                        uint UIntRes = _BusRtuClient.ReadUInt32(FuncAndAdress).Content;
                        return UIntRes;
                    case ModbusRtuDataType.ModbusTcpFloat:
                        float FloatRes = _BusRtuClient.ReadFloat(FuncAndAdress).Content;
                        return FloatRes;
                    case ModbusRtuDataType.ModbusTcpInt64:
                        long LongRes = _BusRtuClient.ReadInt64(FuncAndAdress).Content;
                        return LongRes;
                    case ModbusRtuDataType.ModbusTcpUInt64:
                        ulong ULongRes = _BusRtuClient.ReadUInt64(FuncAndAdress).Content;
                        return ULongRes;
                    case ModbusRtuDataType.ModbusTcpDouble:
                        double DoubleRes = _BusRtuClient.ReadDouble(FuncAndAdress).Content;
                        return DoubleRes;
                    case ModbusRtuDataType.ModbusTcpString:
                        string StringRes = _BusRtuClient.ReadString(FuncAndAdress, Count).Content;
                        return StringRes;
                    default:
                        return null;
                }
            }
            catch (Exception)
            {
                return null;
                throw;
            }

        }
        #endregion

        #region 写入
        /// <summary>
        /// 写入
        /// </summary>
        /// <param name="DataType">数据类型</param>
        /// <param name="FuncValue">功能码</param>
        /// <param name="Adress">地址</param>
        /// <param name="Value">值</param>
        /// <param name="Count">字符串个数，读字符串才填写</param>
        /// <returns></returns>
        public bool Write(ModbusRtuDataType DataType, int FuncValue, uint Adress, object Value, ushort Count = 0)
        {
            //0x05    写一个线圈操作，
            //0x06    写一个寄存器值，
            //0x0F    批量写线圈操作，
            //0x10    批量写寄存器值，
            //0x16    掩码写入的寄存器（大部分的modbus设备不支持）
            _BusRtuClient.DataFormat = _DataFormat;//格式
            string FuncAndAdress = "x=" + FuncValue + ";" + Adress;
            OperateResult res = new OperateResult();
            try
            {
                switch (DataType)
                {
                    case ModbusRtuDataType.ModbusTcpBool:
                        res = _BusRtuClient.Write(FuncAndAdress, (bool)Value);
                        break;
                    case ModbusRtuDataType.ModbusTcpInt16:
                        res = _BusRtuClient.Write(FuncAndAdress, (short)Value);
                        break;
                    case ModbusRtuDataType.ModbusTcpUInt16:
                        res = _BusRtuClient.Write(FuncAndAdress, (ushort)Value);
                        break;
                    case ModbusRtuDataType.ModbusTcpInt32:
                        res = _BusRtuClient.Write(FuncAndAdress, (int)Value);
                        break;
                    case ModbusRtuDataType.ModbusTcpUInt32:
                        res = _BusRtuClient.Write(FuncAndAdress, (uint)Value);
                        break;
                    case ModbusRtuDataType.ModbusTcpFloat:
                        res = _BusRtuClient.Write(FuncAndAdress, (float)Value);
                        break;
                    case ModbusRtuDataType.ModbusTcpInt64:
                        res = _BusRtuClient.Write(FuncAndAdress, (long)Value);
                        break;
                    case ModbusRtuDataType.ModbusTcpUInt64:
                        res = _BusRtuClient.Write(FuncAndAdress, (ulong)Value);
                        break;
                    case ModbusRtuDataType.ModbusTcpDouble:
                        res = _BusRtuClient.Write(FuncAndAdress, (double)Value);
                        break;
                    case ModbusRtuDataType.ModbusTcpString:
                        res = _BusRtuClient.Write(FuncAndAdress, (string)Value);
                        break;
                    default:
                        return false;
                }
                if (res.IsSuccess)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception)
            {
                return false;
                throw;
            }
        }
        #endregion

        #region 读批量
        /// <summary>
        /// 读批量
        /// </summary>
        /// <param name="DataType">数值类型</param>
        /// <param name="FuncValue">功能码</param>
        /// <param name="Adress">地址</param>
        /// <param name="Count">读取个数</param>
        /// <returns></returns>
        public object[] ReadArray(ModbusRtuDataType DataType, int FuncValue, uint Adress, ushort Count)
        {
            //FuncValue功能码
            //0x01    读取线圈的操作，
            //0x02    读取离散的操作，
            //0x03    读取寄存器的值，
            //0x04    读取输入寄存器的值,
            _BusRtuClient.DataFormat = _DataFormat;//格式
            string FuncAndAdress = "x=" + FuncValue + ";" + Adress;
            try
            {
                switch (DataType)
                {
                    case ModbusRtuDataType.ModbusTcpBool:
                        bool[] BoolRes = _BusRtuClient.ReadBool(FuncAndAdress, Count).Content;
                        if (BoolRes == null)
                        {
                            return null;
                        }
                        object[] ObjBoolRes = new object[BoolRes.Length];
                        BoolRes.CopyTo(ObjBoolRes, 0);
                        return ObjBoolRes;
                    case ModbusRtuDataType.ModbusTcpInt16:
                        short[] ShortRes = _BusRtuClient.ReadInt16(FuncAndAdress, Count).Content;
                        if (ShortRes == null)
                        {
                            return null;
                        }
                        object[] ObjShortRes = new object[ShortRes.Length];
                        ShortRes.CopyTo(ObjShortRes, 0);
                        return ObjShortRes;
                    case ModbusRtuDataType.ModbusTcpUInt16:
                        ushort[] UShortRes = _BusRtuClient.ReadUInt16(FuncAndAdress, Count).Content;
                        if (UShortRes == null)
                        {
                            return null;
                        }
                        object[] ObjUShortRes = new object[UShortRes.Length];
                        UShortRes.CopyTo(ObjUShortRes, 0);
                        return ObjUShortRes;
                    case ModbusRtuDataType.ModbusTcpInt32:
                        int[] IntRes = _BusRtuClient.ReadInt32(FuncAndAdress, Count).Content;
                        if (IntRes == null)
                        {
                            return null;
                        }
                        object[] ObjIntRes = new object[IntRes.Length];
                        IntRes.CopyTo(ObjIntRes, 0);
                        return ObjIntRes;
                    case ModbusRtuDataType.ModbusTcpUInt32:
                        uint[] UIntRes = _BusRtuClient.ReadUInt32(FuncAndAdress, Count).Content;
                        if (UIntRes == null)
                        {
                            return null;
                        }
                        object[] ObjUIntRes = new object[UIntRes.Length];
                        UIntRes.CopyTo(ObjUIntRes, 0);
                        return ObjUIntRes;
                    case ModbusRtuDataType.ModbusTcpFloat:
                        float[] FloatRes = _BusRtuClient.ReadFloat(FuncAndAdress, Count).Content;
                        if (FloatRes == null)
                        {
                            return null;
                        }
                        object[] ObjFloatRes = new object[FloatRes.Length];
                        FloatRes.CopyTo(ObjFloatRes, 0);
                        return ObjFloatRes;
                    case ModbusRtuDataType.ModbusTcpInt64:
                        long[] LongRes = _BusRtuClient.ReadInt64(FuncAndAdress, Count).Content;
                        if (LongRes == null)
                        {
                            return null;
                        }
                        object[] ObjLongRes = new object[LongRes.Length];
                        LongRes.CopyTo(ObjLongRes, 0);
                        return ObjLongRes;
                    case ModbusRtuDataType.ModbusTcpUInt64:
                        ulong[] ULongRes = _BusRtuClient.ReadUInt64(FuncAndAdress, Count).Content;
                        if (ULongRes == null)
                        {
                            return null;
                        }
                        object[] ObjULongRes = new object[ULongRes.Length];
                        ULongRes.CopyTo(ObjULongRes, 0);
                        return ObjULongRes;
                    case ModbusRtuDataType.ModbusTcpDouble:
                        double[] DoubleRes = _BusRtuClient.ReadDouble(FuncAndAdress, Count).Content;
                        if (DoubleRes == null)
                        {
                            return null;
                        }
                        object[] ObjDoubleRes = new object[DoubleRes.Length];
                        DoubleRes.CopyTo(ObjDoubleRes, 0);
                        return ObjDoubleRes;
                    default:
                        return null;
                }
            }
            catch (Exception)
            {
                return null;
                throw;
            }

        }
        #endregion

        #region 写入批量
        /// <summary>
        /// 写入批量
        /// </summary>
        /// <param name="DataType">数据类型</param>
        /// <param name="FuncValue">功能码</param>
        /// <param name="Adress">地址</param>
        /// <param name="Value">值</param>
        /// <param name="Count">写入个数</param>
        /// <returns></returns>
        public bool WriteArray(ModbusRtuDataType DataType, int FuncValue, uint Adress, object[] Value, ushort Count)
        {
            _BusRtuClient.DataFormat = _DataFormat;//格式
            string FuncAndAdress = "x=" + FuncValue + ";" + Adress;
            OperateResult res = new OperateResult();
            try
            {
                switch (DataType)
                {
                    case ModbusRtuDataType.ModbusTcpBool:
                        bool[] BoolArray = new bool[Value.Length];
                        Value.CopyTo(BoolArray, 0);
                        res = _BusRtuClient.Write(FuncAndAdress, BoolArray);
                        break;
                    case ModbusRtuDataType.ModbusTcpInt16:
                        short[] Int16Array = new short[Value.Length];
                        Value.CopyTo(Int16Array, 0);
                        res = _BusRtuClient.Write(FuncAndAdress, Int16Array);
                        break;
                    case ModbusRtuDataType.ModbusTcpUInt16:
                        ushort[] UInt16Array = new ushort[Value.Length];
                        Value.CopyTo(UInt16Array, 0);
                        res = _BusRtuClient.Write(FuncAndAdress, UInt16Array);
                        break;
                    case ModbusRtuDataType.ModbusTcpInt32:
                        int[] Int32Array = new int[Value.Length];
                        Value.CopyTo(Int32Array, 0);
                        res = _BusRtuClient.Write(FuncAndAdress, Int32Array);
                        break;
                    case ModbusRtuDataType.ModbusTcpUInt32:
                        uint[] UInt32Array = new uint[Value.Length];
                        Value.CopyTo(UInt32Array, 0);
                        res = _BusRtuClient.Write(FuncAndAdress, UInt32Array);
                        break;
                    case ModbusRtuDataType.ModbusTcpFloat:
                        float[] FloatArray = new float[Value.Length];
                        Value.CopyTo(FloatArray, 0);
                        res = _BusRtuClient.Write(FuncAndAdress, FloatArray);
                        break;
                    case ModbusRtuDataType.ModbusTcpInt64:
                        long[] Int64Array = new long[Value.Length];
                        Value.CopyTo(Int64Array, 0);
                        res = _BusRtuClient.Write(FuncAndAdress, Int64Array);
                        break;
                    case ModbusRtuDataType.ModbusTcpUInt64:
                        ulong[] UInt64Array = new ulong[Value.Length];
                        Value.CopyTo(UInt64Array, 0);
                        res = _BusRtuClient.Write(FuncAndAdress, UInt64Array);
                        break;
                    case ModbusRtuDataType.ModbusTcpDouble:
                        double[] DoubleArray = new double[Value.Length];
                        Value.CopyTo(DoubleArray, 0);
                        res = _BusRtuClient.Write(FuncAndAdress, DoubleArray);
                        break;
                    default:
                        return false;
                }
                if (res.IsSuccess)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception)
            {
                return false;
                throw;
            }
        }
        #endregion

        #region 读(泛型单一)
        /// <summary>
        /// 读(泛型单一)
        /// </summary>
        /// <param name="DataType">数值类型</param>
        /// <param name="FuncValue">功能码</param>
        /// <param name="Adress">地址</param>
        /// <param name="Count">字符串个数，读字符串才填写</param>
        /// <returns></returns>
        public T Read<T>( int FuncValue, uint Adress, ushort Count = 0)
        {
            //FuncValue功能码
            //0x01    读取线圈的操作，
            //0x02    读取离散的操作，
            //0x03    读取寄存器的值，
            //0x04    读取输入寄存器的值,
            _BusRtuClient.DataFormat = _DataFormat;//格式
            string FuncAndAdress = "x=" + FuncValue + ";" + Adress;
            try
            {
                switch (typeof(T).Name)
                {
                    case "Boolean":
                        object BoolRes = _BusRtuClient.ReadBool(FuncAndAdress).Content;
                        return (T)BoolRes;
                    case "Int16":
                        object ShortRes = _BusRtuClient.ReadInt16(FuncAndAdress).Content;
                        return (T)ShortRes;
                    case "UInt16":
                        object UShortRes = _BusRtuClient.ReadUInt16(FuncAndAdress).Content;
                        return (T)UShortRes;
                    case "Int32":
                        object IntRes = _BusRtuClient.ReadInt32(FuncAndAdress).Content;
                        return (T)IntRes;
                    case "UInt32":
                        object UIntRes = _BusRtuClient.ReadUInt32(FuncAndAdress).Content;
                        return (T)UIntRes;
                    case "Single":
                        object FloatRes = _BusRtuClient.ReadFloat(FuncAndAdress).Content;
                        return (T)FloatRes;
                    case "Int64":
                        object LongRes = _BusRtuClient.ReadInt64(FuncAndAdress).Content;
                        return (T)LongRes;
                    case "UInt64":
                        object ULongRes = _BusRtuClient.ReadUInt64(FuncAndAdress).Content;
                        return (T)ULongRes;
                    case "Double":
                        object DoubleRes = _BusRtuClient.ReadDouble(FuncAndAdress).Content;
                        return (T)DoubleRes;
                    case "String":
                        object StringRes = _BusRtuClient.ReadString(FuncAndAdress, Count).Content;
                        return (T)StringRes;
                    default:
                        return default(T);
                }
            }
            catch (Exception)
            {
                return default(T);
            }

        }
        #endregion

        #region 读(泛型批量)
        /// <summary>
        /// 读(泛型批量)
        /// </summary>
        /// <param name="DataType">数值类型</param>
        /// <param name="FuncValue">功能码</param>
        /// <param name="Adress">地址</param>
        /// <param name="Count">读取个数</param>
        /// <returns></returns>
        public List<T> ReadArray<T>( int FuncValue, uint Adress, ushort Count)
        {
            //FuncValue功能码
            //0x01    读取线圈的操作，
            //0x02    读取离散的操作，
            //0x03    读取寄存器的值，
            //0x04    读取输入寄存器的值,
            _BusRtuClient.DataFormat = _DataFormat;//格式
            string FuncAndAdress = "x=" + FuncValue + ";" + Adress;
            try
            {
                switch (typeof(T).Name)
                {
                    case "Boolean":
                        bool[] BoolRes = _BusRtuClient.ReadBool(FuncAndAdress, Count).Content;
                        if (BoolRes == null)
                        {
                            return null;
                        }
                        object[] ObjBoolRes = new object[BoolRes.Length];
                        BoolRes.CopyTo(ObjBoolRes, 0);
                        return ConvertList<List<T>>(ObjBoolRes);
                    case "Int16":
                        short[] ShortRes = _BusRtuClient.ReadInt16(FuncAndAdress, Count).Content;
                        if (ShortRes == null)
                        {
                            return null;
                        }
                        object[] ObjShortRes = new object[ShortRes.Length];
                        ShortRes.CopyTo(ObjShortRes, 0);
                        return ConvertList<List<T>>(ObjShortRes);
                    case "UInt16":
                        ushort[] UShortRes = _BusRtuClient.ReadUInt16(FuncAndAdress, Count).Content;
                        if (UShortRes == null)
                        {
                            return null;
                        }
                        object[] ObjUShortRes = new object[UShortRes.Length];
                        UShortRes.CopyTo(ObjUShortRes, 0);
                        return ConvertList<List<T>>(ObjUShortRes);
                    case "Int32":
                        int[] IntRes = _BusRtuClient.ReadInt32(FuncAndAdress, Count).Content;
                        if (IntRes == null)
                        {
                            return null;
                        }
                        object[] ObjIntRes = new object[IntRes.Length];
                        IntRes.CopyTo(ObjIntRes, 0);
                        return ConvertList<List<T>>(ObjIntRes);
                    case "UInt32":
                        uint[] UIntRes = _BusRtuClient.ReadUInt32(FuncAndAdress, Count).Content;
                        if (UIntRes == null)
                        {
                            return null;
                        }
                        object[] ObjUIntRes = new object[UIntRes.Length];
                        UIntRes.CopyTo(ObjUIntRes, 0);
                        return ConvertList<List<T>>(ObjUIntRes);
                    case "Single":
                        float[] FloatRes = _BusRtuClient.ReadFloat(FuncAndAdress, Count).Content;
                        if (FloatRes == null)
                        {
                            return null;
                        }
                        object[] ObjFloatRes = new object[FloatRes.Length];
                        FloatRes.CopyTo(ObjFloatRes, 0);
                        return ConvertList<List<T>>(ObjFloatRes);
                    case "Int64":
                        long[] LongRes = _BusRtuClient.ReadInt64(FuncAndAdress, Count).Content;
                        if (LongRes == null)
                        {
                            return null;
                        }
                        object[] ObjLongRes = new object[LongRes.Length];
                        LongRes.CopyTo(ObjLongRes, 0);
                        return ConvertList<List<T>>(ObjLongRes);
                    case "UInt64":
                        ulong[] ULongRes = _BusRtuClient.ReadUInt64(FuncAndAdress, Count).Content;
                        if (ULongRes == null)
                        {
                            return null;
                        }
                        object[] ObjULongRes = new object[ULongRes.Length];
                        ULongRes.CopyTo(ObjULongRes, 0);
                        return ConvertList<List<T>>(ObjULongRes);
                    case "Double":
                        double[] DoubleRes = _BusRtuClient.ReadDouble(FuncAndAdress, Count).Content;
                        if (DoubleRes == null)
                        {
                            return null;
                        }
                        object[] ObjDoubleRes = new object[DoubleRes.Length];
                        DoubleRes.CopyTo(ObjDoubleRes, 0);
                        return ConvertList<List<T>>(ObjDoubleRes);
                    default:
                        return default;
                }
            }
            catch (Exception)
            {
                return default;
                throw;
            }

        }
        #endregion

        #region 写入(泛型单一与批量)
        /// <summary>
        /// 写入(泛型单一与批量)
        /// </summary>
        /// <param name="DataType">数据类型</param>
        /// <param name="FuncValue">功能码</param>
        /// <param name="Adress">地址</param>
        /// <param name="Value">值</param>
        /// <param name="Count">字符串个数，读字符串才填写</param>
        /// <returns></returns>
        public bool Write<T>(int FuncValue, uint Adress, T Value, ushort Count = 0)
        {
            //0x05    写一个线圈操作，
            //0x06    写一个寄存器值，
            //0x0F    批量写线圈操作，
            //0x10    批量写寄存器值，
            //0x16    掩码写入的寄存器（大部分的modbus设备不支持）
            _BusRtuClient.DataFormat = _DataFormat;//格式
            string FuncAndAdress = "x=" + FuncValue + ";" + Adress;
            OperateResult res = new OperateResult();
            try
            {
                switch (typeof(T).Name)
                {
                    case "Boolean":
                        res = _BusRtuClient.Write(FuncAndAdress, Convert.ToBoolean(Value));
                        break;
                    case "Int16":
                        res = _BusRtuClient.Write(FuncAndAdress, Convert.ToInt16(Value));
                        break;
                    case "UInt16":
                        res = _BusRtuClient.Write(FuncAndAdress, Convert.ToUInt16(Value));
                        break;
                    case "Int32":
                        res = _BusRtuClient.Write(FuncAndAdress, Convert.ToInt32(Value));
                        break;
                    case "UInt32":
                        res = _BusRtuClient.Write(FuncAndAdress, Convert.ToUInt32(Value));
                        break;
                    case "Single":
                        res = _BusRtuClient.Write(FuncAndAdress, Convert.ToSingle(Value));
                        break;
                    case "Int64":
                        res = _BusRtuClient.Write(FuncAndAdress, Convert.ToInt64(Value));
                        break;
                    case "UInt64":
                        res = _BusRtuClient.Write(FuncAndAdress, Convert.ToUInt64(Value));
                        break;
                    case "Double":
                        res = _BusRtuClient.Write(FuncAndAdress, Convert.ToDouble(Value));
                        break;
                    case "String":
                        res = _BusRtuClient.Write(FuncAndAdress, Convert.ToString(Value));
                        break;
                    case "Boolean[]":
                        bool[] BoolArray = (bool[])(Value as Array);
                        res = _BusRtuClient.Write(FuncAndAdress, BoolArray);
                        break;
                    case "Int16[]":
                        short[] Int16Array = (short[])(Value as Array);
                        res = _BusRtuClient.Write(FuncAndAdress, Int16Array);
                        break;
                    case"UInt16[]":
                        ushort[] UInt16Array = (ushort[])(Value as Array);
                        res = _BusRtuClient.Write(FuncAndAdress, UInt16Array);
                        break;
                    case"Int32[]":
                        int[] Int32Array = (int[])(Value as Array);
                        res = _BusRtuClient.Write(FuncAndAdress, Int32Array);
                        break;
                    case "UInt32[]":
                        uint[] UInt32Array = (uint[])(Value as Array);
                        res = _BusRtuClient.Write(FuncAndAdress, UInt32Array);
                        break;
                    case"Single[]":
                        float[] FloatArray = (float[])(Value as Array);
                        res = _BusRtuClient.Write(FuncAndAdress, FloatArray);
                        break;
                    case "Int64[]":
                        long[] Int64Array = (long[])(Value as Array);
                        res = _BusRtuClient.Write(FuncAndAdress, Int64Array);
                        break;
                    case "UInt64[]":
                        ulong[] UInt64Array = (ulong[])(Value as Array);
                        res = _BusRtuClient.Write(FuncAndAdress, UInt64Array);
                        break;
                    case "Double[]":
                        double[] DoubleArray = (double[])(Value as Array);
                        res = _BusRtuClient.Write(FuncAndAdress, DoubleArray);
                        break;
                    default:
                        return false;
                }
                if (res.IsSuccess)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception)
            {
                return false;
                throw;
            }
        }
        #endregion

        #region object数组转泛型集合
        //object数组转泛型集合
        public T ConvertList<T>(object obj)
        {
            if (obj is object[])
            {
                object[] objects = (object[])obj;
                // Here we create an instance of type T, where T is actually a generic
                // List<TElement>. Then we cast it to type IList. Therefore here we don't 
                // have to know and specify the type of the elements of List<TElement>.
                IList list = (IList)Activator.CreateInstance(typeof(T));

                foreach (object o in objects)
                    // Here we call IList.Add(object o). Internally it casts added
                    // elements to the actual type of the elements of List<TElement>.
                    list.Add(o);

                return (T)list;
            }
            return default;
        }
        #endregion
    }
    public enum ModbusRtuDataType//plc数据类型
    {
        ModbusTcpBool,
        ModbusTcpInt16,
        ModbusTcpUInt16,
        ModbusTcpInt32,
        ModbusTcpUInt32,
        ModbusTcpFloat,
        ModbusTcpInt64,
        ModbusTcpUInt64,
        ModbusTcpDouble,
        ModbusTcpString
    }
}
